﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;

namespace MyWeiBoWeb.Controllers
{
    public class UserController : Controller
    {
        //加载用户信息
        public ActionResult UserInfo()
        {
            UserInfo UserInfo = LoginManager.GetUserInfo(LoginManager.CentreLogin);
            Users user = LoginManager.GetUsers(LoginManager.CentreLogin);
            ViewBag.UserInfo = UserInfo;
            ViewBag.user = user;
            return View();
        }

        //修改用户信息
        public ActionResult AlterUserInfo()
        {
            try
            {
                using (MicroBlogEntities db = new MicroBlogEntities())
                {
                    int UserID = db.Users.FirstOrDefault(u => u.U_Email == LoginManager.CentreLogin).U_ID;
                    var userInfo = db.UserInfo.FirstOrDefault(u => u.U_ID == UserID);
                    var user = db.Users.FirstOrDefault(p => p.U_Email == LoginManager.CentreLogin);
                    user.U_Nick = Request["user_U_Nick"];
                    userInfo.UserInfo_realname = Request["UserInfo_realname"];
                    userInfo.UserInfo_Address = Request["province"] + Request["city"];
                    userInfo.UserInfo_Sex = Request["Fruit"];
                    db.Configuration.ValidateOnSaveEnabled = false;
                    db.SaveChanges();
                }
                return Content("<script>alert('更新成功');location.href='/User/UserInfo'</script>");
            }
            catch (Exception)
            {
                return Content("<script>alert('更新失败');location.href='/User/UserInfo'</script>");
                throw;
            }

        }

        //修改密码
        public ActionResult AlterPwd()
        {
            Users user = new Users()
            {
                U_Email = LoginManager.CentreLogin,
                U_Pwd = Request["OldPwd"]
            };
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                var list = db.Users.FirstOrDefault(u => u.U_Pwd == user.U_Pwd && u.U_Email == user.U_Email);
                if (list != null && Request["NewPwd"] != "" && Request["NewPwdAgin"] == Request["NewPwd"])
                {
                    try
                    {
                        var users = db.Users.FirstOrDefault(p => p.U_Email == LoginManager.CentreLogin);
                        users.U_Pwd = Request["NewPwd"];
                        db.Configuration.ValidateOnSaveEnabled = false;
                        db.SaveChanges();
                        return Content("<script>alert('修改成功');location.href='/User/UserInfo'</script>");
                    }
                    catch (Exception)
                    {
                        return Content("<script>alert('修改失败');location.href='/User/UserInfo'</script>");
                        throw;
                    }

                }
                else if (Request["NewPwd"] != Request["NewPwdAgin"])
                {
                    return Content("<script>alert('请重新确认密码！！！');location.href='/User/UserInfo'</script>");
                }
                else
                {
                    return Content("<script>alert('原密码错误！！！');location.href='/User/UserInfo'</script>");
                }
            }
        }

        //修改教育信息
        public ActionResult AlterEdu()
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                try
                {
                    int UserID = db.Users.FirstOrDefault(u => u.U_Email == LoginManager.CentreLogin).U_ID;
                    var userInfo = db.UserInfo.FirstOrDefault(u => u.U_ID == UserID);
                    userInfo.UserInfo_SchoolName = Request["UserInfo_SchoolName"];
                    userInfo.UserInfo_Department = Request["UserInfo_Department"];
                    db.Configuration.ValidateOnSaveEnabled = false;
                    db.SaveChanges();
                    return Content("<script>alert('更新成功');location.href='/User/UserInfo'</script>");
                }
                catch (Exception)
                {
                    return Content("<script>alert('更新失败');location.href='/User/UserInfo'</script>");
                    throw;
                }

            }
        }

        //修改职业信息
        public ActionResult AlterPro()
        {
            using (MicroBlogEntities db = new MicroBlogEntities())
            {
                try
                {
                    int UserID = db.Users.FirstOrDefault(u => u.U_Email == LoginManager.CentreLogin).U_ID;
                    var userInfo = db.UserInfo.FirstOrDefault(u => u.U_ID == UserID);
                    userInfo.UserInfo_Location = Request["UserInfo_Location"];
                    userInfo.UserInfo_CompanyName = Request["UserInfo_CompanyName"];
                    userInfo.UserInfo_DepartmentPosition = Request["UserInfo_DepartmentPosition"];
                    db.Configuration.ValidateOnSaveEnabled = false;
                    db.SaveChanges();
                    return Content("<script>alert('更新成功');location.href='/User/UserInfo'</script>");
                }
                catch (Exception)
                {
                    return Content("<script>alert('更新失败');location.href='/User/UserInfo'</script>");
                    throw;
                }
            }
        }
    }
}